<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="讲者姓名" field="name">
        <a-input v-model="formData.name" placeholder="请输入讲者姓名" />
      </a-form-item>
      <a-form-item label="讲者姓名-英文" field="name_en">
        <a-input v-model="formData.name_en" placeholder="请输入讲者姓名-英文" />
      </a-form-item>
      <a-form-item label="头衔" field="title">
        <a-input v-model="formData.title" placeholder="请输入头衔" />
      </a-form-item>
      <a-form-item label="国家" field="country">
        <a-input v-model="formData.country" placeholder="请输入国家" />
      </a-form-item>
      <a-form-item label="城市" field="city">
        <a-input v-model="formData.city" placeholder="请输入城市" />
      </a-form-item>
      <a-form-item label="国家—英文" field="country_en">
        <a-input v-model="formData.country_en" placeholder="请输入国家—英文" />
      </a-form-item>
      <a-form-item label="城市-英文" field="city_en">
        <a-input v-model="formData.city_en" placeholder="请输入城市-英文" />
      </a-form-item>
      <a-form-item label="时区" field="time_zone">
        <a-input v-model="formData.time_zone" placeholder="请输入时区" />
      </a-form-item>
      <a-form-item label="时差" field="jet_lag">
        <a-input v-model="formData.jet_lag" placeholder="请输入时差" />
      </a-form-item>
      <a-form-item label="就职机构及任职" field="institution">
        <ma-wangEditor v-model="formData.institution" :height="400" />
      </a-form-item>
      <a-form-item label="就职机构及任职-英文" field="institution_en">
        <ma-wangEditor v-model="formData.institution_en" :height="400" />
      </a-form-item>
      <a-form-item label="擅长领域" field="areas_of_expertise">
        <ma-wangEditor v-model="formData.areas_of_expertise" :height="400" />
      </a-form-item>
      <a-form-item label="擅长领域-英文" field="areas_of_expertise_en">
        <ma-wangEditor v-model="formData.areas_of_expertise_en" :height="400" />
      </a-form-item>
      <a-form-item label="医疗领域" field="medical_category">
        <ma-wangEditor v-model="formData.medical_category" :height="400" />
      </a-form-item>
      <a-form-item label="医疗领域-英文" field="medical_category_en">
        <ma-wangEditor v-model="formData.medical_category_en" :height="400" />
      </a-form-item>
      <a-form-item label="讲者简介" field="remarks">
        <ma-wangEditor v-model="formData.remarks" :height="400" />
      </a-form-item>
      <a-form-item label="讲者简介-英文" field="remarks_en">
        <ma-wangEditor v-model="formData.remarks_en" :height="400" />
      </a-form-item>
      <a-form-item label="会议演讲及题目" field="presenting_meeting">
        <ma-wangEditor v-model="formData.presenting_meeting" :height="400" />
      </a-form-item>
      <a-form-item label="会议演讲及题目-英文" field="presenting_meeting_en">
        <ma-wangEditor v-model="formData.presenting_meeting_en" :height="400" />
      </a-form-item>
      <a-form-item label="演讲研究赞助商" field="research_sponsor">
        <ma-wangEditor v-model="formData.research_sponsor" :height="400" />
      </a-form-item>
      <a-form-item label="学协会及任职" field="association">
        <ma-wangEditor v-model="formData.association" :height="400" />
      </a-form-item>
      <a-form-item label="学协会及任职" field="association_en">
        <ma-wangEditor v-model="formData.association_en" :height="400" />
      </a-form-item>
      <a-form-item label="期刊及任职及影响因子" field="journal_position">
        <ma-wangEditor v-model="formData.journal_position" :height="400" />
      </a-form-item>
      <a-form-item label="期刊及任职及影响因子-英文" field="journal_position_en">
        <ma-wangEditor v-model="formData.journal_position_en" :height="400" />
      </a-form-item>
      <a-form-item label="信息来源网页" field="information_website">
        <ma-wangEditor v-model="formData.information_website" :height="400" />
      </a-form-item>
      <a-form-item label="头像" field="photo">
        <ma-wangEditor v-model="formData.photo" :height="400" />
      </a-form-item>
      <a-form-item label="邮箱" field="email">
        <a-input v-model="formData.email" placeholder="请输入邮箱" />
      </a-form-item>
      <a-form-item label="备注" field="note">
        <ma-wangEditor v-model="formData.note" :height="400" />
      </a-form-item>
      <a-form-item label="" field="sort">
        <a-input v-model="formData.sort" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="是否前端展示 0隐藏 1显示" field="is_display">
        <a-input v-model="formData.is_display" placeholder="请输入是否前端展示 0隐藏 1显示" />
      </a-form-item>
      <a-form-item label="" field="review_comments">
        <a-input v-model="formData.review_comments" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="自定义标签" field="tags">
        <a-input v-model="formData.tags" placeholder="请输入自定义标签" />
      </a-form-item>
      <a-form-item label="邀约人数" field="number_of_invitations">
        <a-input v-model="formData.number_of_invitations" placeholder="请输入邀约人数" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/speaker'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '外籍讲者库' + (mode == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  name: '',
  name_en: '',
  title: '',
  country: '',
  city: '',
  country_en: '',
  city_en: '',
  time_zone: '',
  jet_lag: '',
  institution: '',
  institution_en: '',
  areas_of_expertise: '',
  areas_of_expertise_en: '',
  medical_category: '',
  medical_category_en: '',
  remarks: '',
  remarks_en: '',
  presenting_meeting: '',
  presenting_meeting_en: '',
  research_sponsor: '',
  association: '',
  association_en: '',
  journal_position: '',
  journal_position_en: '',
  information_website: '',
  photo: '',
  email: '',
  note: '',
  sort: null,
  is_display: null,
  review_comments: '',
  tags: '',
  number_of_invitations: null,
})

// 验证规则
const rules = {
  name: [{ required: true, message: '讲者姓名必需填写' }],
  name_en: [{ required: true, message: '讲者姓名-英文必需填写' }],
  title: [{ required: true, message: '头衔必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
